package cn.netbox.springmvc.Controller;

import cn.netbox.springmvc.dao.UserDao;
import cn.netbox.springmvc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
public class HelloController {

    @Autowired
    private UserDao userDao;

    @RequestMapping("/")
    public  String  Hello(){
    return "index";
    }
        @GetMapping("/getUser")
                public  String getUserById(int id, Model model){
            model.addAttribute("user",userDao.selectUserById(id));
            return "userInfo";
    }

    @GetMapping("/getAllUser")
    public  String  getAllUser(Model model){
        model.addAttribute("userMapSet",userDao.getAllUser());
        return "userlist_info";
    }

    @PostMapping("/user")
    public String createUser(User user, Model model) {
        userDao.create(user);
        return "success";
    }

    @DeleteMapping("/delete")
    public String deleteUser(int id) {
        userDao.deletUserById(id);
        return "success";
    }

    @PutMapping("/updateUser")
    public String updateUser(User user) {
        userDao.updateUser(user);
        return "redirect: /getAllUser";
    }
}
